package com.qyhl.module_practice.order;

import android.annotation.SuppressLint;

import com.google.gson.Gson;
import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.UpTokenBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeOrderListBean;

import java.text.SimpleDateFormat;
import java.util.Date;

public class PracticeOrderModel implements PracticeOrderContract.PracticeOrderModel {
    private PracticeOrderPresenter mPresenter;

    PracticeOrderModel(PracticeOrderPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getTypeList(String instId) {

        EasyHttp.get(PracticeUrl.GET_PRACTICE_ORDER_TYPE)
                .params("instId", instId)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .execute(new SimpleCallBack<PracticeOrderListBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setTypeError("暂无任何类型！");
                        } else {
                            mPresenter.setTypeError("获取类型出错，请稍后再试！");
                        }
                    }

                    @Override
                    public void onSuccess(PracticeOrderListBean practiceOrderListBean) {
                        mPresenter.setTypeList(practiceOrderListBean);
                    }
                });
    }

    @Override
    public void upload(String volId, String insId, String address, String detail, String name, String creator, String type, String imgs, String upPhone) {

        EasyHttp.post(PracticeUrl.POST_PRACTICE_ADD_ORDER)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("volId", volId)
                .params("insId", insId)
                .params("address", address)
                .params("detail", detail)
                .params("name", name)
                .params("creator", creator)
                .params("type", type)
                .params("imgs", imgs)
                .params("phone", upPhone)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.uploadError("上传失败，请稍后再试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult =new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.uploadSuccess("上传成功，等待审核！");
                        } else {
                            mPresenter.uploadError("上传出错，请稍后再试！");
                        }
                    }
                });

    }


    @Override
    public void getToken(final boolean isUpload) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CommonUtils.getInstance().getCircleUrl()+ "?timestamp=" + time;
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite()+ "&method=getUpToken&type=3"))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<UpTokenBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getTokenError(isUpload);
                    }

                    @Override
                    public void onSuccess(UpTokenBean upTokenBean) {
                        mPresenter.getTokenSuccess(upTokenBean, isUpload);
                    }
                });

    }
}

